import {
	ref
} from 'vue'
import {
	toast
} from '../utils/common'
import request from '../utils/request.js'

export const useRefresh = (callback) => {
	const isTriggered = ref(false);
	// 上拉加载时的刷新
	const upPullRefresh = async () => {
		uni.showLoading({
			title: "正在加载中"
		})
		let result = await callback
		isTriggered.value = true
		new Promise((resolve, reject) => {
			result().then(res => {
				if (res.code === 200) {
					uni.stopPullDownRefresh()
					resolve(res.data)
				} else {
					toast({
						title: res.message
					})
					uni.stopPullDownRefresh()
				}
				uni.hideLoading()
				isTriggered.value = false
			}).catch(err => {
				reject()
			})
		})
	}

	return {
		isTriggered,
		upPullRefresh
	}

	// const onScrolltolower = () => {
	// 	console.log(11)
	// 	uni.showLoading({
	// 		title: '正在请求中...'
	// 	})
	// 	setTimeout(() => {
	// 		let arr = guessList.value
	// 		guessList.value.push(...arr)
	// 		uni.hideLoading()
	// 		uni.showToast({
	// 			title: '没有更多数据了'
	// 		});
	// 	}, 3000)
	// }
}